using System;
using Firestarter.Test.Base;
using FireStarterModeller.BO;
using Habanero.Base;
using Habanero.BO;
using NUnit.Framework;

namespace FireStarterModeller.Test.BO
{
    [TestFixture]
    public class TestDMRelationshipSingleToMultiplePropKeyRule
    {
        [SetUp]
        public void SetupTest()
        {
            //Runs every time that any testmethod is executed
            TestUtilsDomain.SetupTest();
            BORegistry.DataAccessor = new DataAccessorInMemory();
        }

        [TestFixtureSetUp]
        public void TestFixtureSetup()
        {
            //Code that is executed before any test is run in this class. If multiple tests
            // are executed then it will still only be called once.
            TestUtilsDomain.SetupFixture();
        }

        [TearDown]
        public void TearDownTest()
        {
            //runs every time any testmethod is complete
        }

        #region DMRelationshipSingleToMultipleCannotBePKorUC

        [Test]
        public void Test_DMRelationshipSingleToMultiplePropKeyRule_ForSingleRelationship()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedDefaultDMClass();
            DMRelationship dmRelationship = dmClass.OwnerRelationships.CreateBusinessObject();
            dmRelationship.Cardinality = Cardinality.Multiple;
            //---------------Assert Precondition----------------
            Assert.AreEqual(Cardinality.Multiple, dmRelationship.Cardinality);
            //---------------Execute Test ----------------------
            IBusinessObjectRule rule = new DMRelationshipSingleToMultipleCannotBePKorUC();
            //---------------Test Result -----------------------
            Assert.IsNotNull(rule);
            Assert.AreEqual("DMRelationshipSingleToMultiplePropKey", rule.Name);
            Assert.AreEqual(ErrorLevel.Warning, rule.ErrorLevel);
        }

        [Test]
        public void Test_DMRelationshipSingleToMultiplePropKeyRule_ForSingleRelationship_NullClassDM()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedDefaultDMClass();
            DMRelationship dmRelationship = dmClass.OwnerRelationships.CreateBusinessObject();
            dmRelationship.Cardinality = Cardinality.Multiple;
            dmRelationship.ClassDM = null;
            //---------------Assert Precondition----------------
            Assert.AreEqual(Cardinality.Multiple, dmRelationship.Cardinality);
            Assert.IsNull(dmRelationship.ClassDM);
            //---------------Execute Test ----------------------
            IBusinessObjectRule rule = new DMRelationshipSingleToMultipleCannotBePKorUC();
            rule.IsValid(dmRelationship);
            //---------------Test Result -----------------------
            Assert.IsNotNull(rule);
            Assert.AreEqual("DMRelationshipSingleToMultiplePropKey", rule.Name);
            Assert.AreEqual(ErrorLevel.Warning, rule.ErrorLevel);
            StringAssert.Contains("The single side of the One To Many Relationship '", rule.Message);
            StringAssert.Contains("'" + dmRelationship + "' on Class '" + dmRelationship.ClassDM + "'", rule.Message);
            StringAssert.Contains("' should not be mapped to a Primary Key or Alternate Key.", rule.Message);
        }

//        [Test]
//        public void Test_CreateDMRelationshipSingleToMultiplePropKeyRule_WhenRelationshipNull_ShouldRaiseError()
//        {
//            //---------------Set up test pack-------------------
//            //---------------Assert Precondition----------------
//            //---------------Execute Test ----------------------
//            try
//            {
//                new DMRelationshipSingleToMultipleCannotBePKorUC(null);
//                Assert.Fail("expected ArgumentNullException");
//            }
//            //---------------Test Result -----------------------
//            catch (ArgumentNullException ex)
//            {
//                StringAssert.Contains("Value cannot be null", ex.Message);
//                StringAssert.Contains("dmRelationship", ex.ParamName);
//            }
//        }


        [Test]
        public void Test_IsValid_True_WhenNoReverseRelationship()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = CreateClassWithAKAndRelationship();
            DMRelationship dmRelationship = dmClass.OwnerRelationships[0];
            SetReverseRelationship(dmRelationship, null);
            IBusinessObjectRule rule = new DMRelationshipSingleToMultipleCannotBePKorUC();
            //---------------Assert Precondition----------------
            Assert.IsNull(dmRelationship.ReverseRelationship);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmRelationship);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }

        [Test]
        public void Test_IsValid_True_WhenSingleToSingle()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = CreateClassWithAKAndRelationship();
            DMRelationship dmRelationship = dmClass.OwnerRelationships[0];
            DMClass relatedClass = CreateClassWithAKAndRelationship();
            SetReverseRelationship(dmRelationship, relatedClass.OwnerRelationships[0]);
//            dmRelationship.ReverseRelationship = relatedClass.OwnerRelationships[0];
            IBusinessObjectRule rule = new DMRelationshipSingleToMultipleCannotBePKorUC();
            //---------------Assert Precondition----------------
            Assert.AreEqual(Cardinality.Single, dmRelationship.Cardinality);
            Assert.AreEqual(Cardinality.Single, dmRelationship.ReverseRelationship.Cardinality);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmRelationship);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }

        [Test]
        public void Test_IsValid_True_WhenMultipleToSingle()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = CreateClassWithAKAndRelationship();
            DMRelationship dmRelationship = dmClass.OwnerRelationships[0];
            dmRelationship.Cardinality = Cardinality.Multiple;
            DMClass relatedClass = CreateClassWithAKAndRelationship();
            SetReverseRelationship(dmRelationship, relatedClass.OwnerRelationships[0]);
            IBusinessObjectRule rule = new DMRelationshipSingleToMultipleCannotBePKorUC();
            //---------------Assert Precondition----------------
            Assert.AreEqual(Cardinality.Multiple, dmRelationship.Cardinality);
            Assert.AreEqual(Cardinality.Single, dmRelationship.ReverseRelationship.Cardinality);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmRelationship);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }

        [Test]
        public void Test_IsValid_True_WhenMultipleToMultiple()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = CreateClassWithAKAndRelationship();
            DMRelationship dmRelationship = dmClass.OwnerRelationships[0];
            dmRelationship.Cardinality = Cardinality.Multiple;
            DMClass relatedClass = CreateClassWithAKAndRelationship();
            DMRelationship reverseRelationship = relatedClass.OwnerRelationships[0];
            reverseRelationship.Cardinality = Cardinality.Multiple;
            SetReverseRelationship(dmRelationship, reverseRelationship);
            IBusinessObjectRule rule = new DMRelationshipSingleToMultipleCannotBePKorUC();
            //---------------Assert Precondition----------------
            Assert.AreEqual(Cardinality.Multiple, dmRelationship.Cardinality);
            Assert.AreEqual(Cardinality.Multiple, dmRelationship.ReverseRelationship.Cardinality);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmRelationship);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }

        private static void SetReverseRelationship(DMRelationship dmRelationship, DMRelationship reverseRelationship)
        {
            dmRelationship.Relationships.SetRelatedObject("ReverseRelationship", reverseRelationship);
        }

        [Test]
        public void Test_IsValid_False_SingleToMultiple_WithPKRelProp()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = CreateClassWithPKAndRelationship();
            DMClass relatedClass = CreateClassWithAKAndRelationship();
            DMRelationship dmRelationship = dmClass.OwnerRelationships[0];
            dmRelationship.Cardinality = Cardinality.Single;
            DMRelationship reverseRelationship = relatedClass.OwnerRelationships[0];
            SetReverseRelationship(dmRelationship, reverseRelationship);
//            dmRelationship.ReverseRelationship = reverseRelationship;
            dmRelationship.ReverseRelationship.Cardinality = Cardinality.Multiple;
            IBusinessObjectRule rule = new DMRelationshipSingleToMultipleCannotBePKorUC();
            //---------------Assert Precondition----------------
            Assert.AreEqual(Cardinality.Single, dmRelationship.Cardinality);
            Assert.AreEqual(Cardinality.Multiple, dmRelationship.ReverseRelationship.Cardinality);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmRelationship);
            //---------------Test Result -----------------------
            Assert.IsFalse(isValid);
            TestUtilsShared.AssertStringNotEmpty(rule.Message, "rule.Message");
        }

        [Test]
        public void Test_IsValid_False_SingleWhenMultipleReverse_WithAKRelProp()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = CreateClassWithAKAndRelationship();
            DMClass relatedClass = CreateClassWithAKAndRelationship();
            DMRelationship dmRelationship = dmClass.OwnerRelationships[0];
            dmRelationship.Cardinality = Cardinality.Single;
            DMRelationship reverseRelationship = relatedClass.OwnerRelationships[0];
            SetReverseRelationship(dmRelationship, reverseRelationship);
//            dmRelationship.ReverseRelationship = reverseRelationship;
            dmRelationship.ReverseRelationship.Cardinality = Cardinality.Multiple;
            IBusinessObjectRule rule = new DMRelationshipSingleToMultipleCannotBePKorUC();
            //---------------Assert Precondition----------------
            Assert.AreEqual(Cardinality.Single, dmRelationship.Cardinality);
            Assert.AreEqual(Cardinality.Multiple, dmRelationship.ReverseRelationship.Cardinality);
            Assert.IsTrue(dmRelationship.HasAllAkProps());
            Assert.IsFalse(dmRelationship.HasAllPkProps());
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmRelationship);
            //---------------Test Result -----------------------
            Assert.IsFalse(isValid);
            TestUtilsShared.AssertStringNotEmpty(rule.Message, "rule.Message");
        }

        [Test]
        public void Test_IsValid_True_SingleToMultiple_WithNoKeyRelProp()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = CreateClassWithRelationship();
            DMClass relatedClass = CreateClassWithAKAndRelationship();
            DMRelationship dmRelationship = dmClass.OwnerRelationships[0];
            dmRelationship.Cardinality = Cardinality.Single;
            DMRelationship reverseRelationship = relatedClass.OwnerRelationships[0];
            SetReverseRelationship(dmRelationship, reverseRelationship);
//            dmRelationship.ReverseRelationship = reverseRelationship;
            dmRelationship.ReverseRelationship.Cardinality = Cardinality.Multiple;
            IBusinessObjectRule rule = new DMRelationshipSingleToMultipleCannotBePKorUC();
            //---------------Assert Precondition----------------
            Assert.AreEqual(Cardinality.Single, dmRelationship.Cardinality);
            Assert.AreEqual(Cardinality.Multiple, dmRelationship.ReverseRelationship.Cardinality);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmRelationship);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }

        [Test]
        public void Test_IsValid_True_SingleToMultiple_NoReverseRelatedClass()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = CreateClassWithPKAndRelationship();
            DMRelationship dmRelationship = dmClass.OwnerRelationships[0];
            SetReverseRelationship(dmRelationship, new DMRelationship());
//            dmRelationship.ReverseRelationship = new DMRelationship();
            dmRelationship.ReverseRelationship.Cardinality = Cardinality.Multiple;
            IBusinessObjectRule rule = new DMRelationshipSingleToMultipleCannotBePKorUC();
            //---------------Assert Precondition----------------
            Assert.IsNull(dmRelationship.ReverseRelationship.ClassDM);
            Assert.IsNotNull(dmClass.ObjectIdentity);
            Assert.AreEqual(Cardinality.Single, dmRelationship.Cardinality);
            Assert.AreEqual(Cardinality.Multiple, dmRelationship.ReverseRelationship.Cardinality);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmRelationship);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }

        [Test]
        public void Test_IsValid_True_WhenNoClassDM()
        {
            //---------------Set up test pack-------------------
            DMRelationship dmRelationship = new DMRelationship();
            dmRelationship.Cardinality =Cardinality.Single;
            DMRelationship reverseRelationship = new DMRelationship();
            reverseRelationship.Cardinality = Cardinality.Multiple;
            reverseRelationship.ClassDM = new DMClass();
            SetReverseRelationship(dmRelationship, reverseRelationship);   
            IBusinessObjectRule rule = new DMRelationshipSingleToMultipleCannotBePKorUC();
            //---------------Assert Precondition----------------
            Assert.AreEqual(Cardinality.Single, dmRelationship.Cardinality);
            Assert.AreEqual(Cardinality.Multiple, dmRelationship.ReverseRelationship.Cardinality);
            Assert.IsNull(dmRelationship.ClassDM);
            Assert.IsNotNull(dmRelationship.ReverseRelationship);
            Assert.IsNotNull(dmRelationship.ReverseRelationship.ClassDM);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmRelationship);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }

        private static DMClass CreateClassWithAKAndRelationship()
        {
            DMProperty dmProperty = TestUtilsShared.CreateUnsavedValidDMProperty();
            var dmClass = dmProperty.Class;
            DMRelationship dmRelationship = new DMRelationship();
            dmClass.OwnerRelationships.Add(dmRelationship);
            DMRelationshipProperty relationshipProperty = dmRelationship.RelationshipProperties.CreateBusinessObject();
            relationshipProperty.Property = dmProperty;
            DMUniqueConstraint uniqueConstraint = dmClass.UniqueConstraints.CreateBusinessObject();
            uniqueConstraint.AddPropertyInfo(dmProperty);
            return (DMClass) dmClass;
        }

        private static DMClass CreateClassWithPKAndRelationship()
        {
            DMProperty dmProperty = TestUtilsShared.CreateUnsavedValidDMProperty();
            DMClass dmClass = (DMClass) dmProperty.Class;
            dmClass.CreateObjectIdentity(dmProperty);
            DMRelationship dmRelationship = new DMRelationship();
            dmClass.OwnerRelationships.Add(dmRelationship);
            DMRelationshipProperty relationshipProperty = dmRelationship.RelationshipProperties.CreateBusinessObject();
            relationshipProperty.Property = dmProperty;
            return dmClass;
        }

        private static DMClass CreateClassWithRelationship()
        {
            DMProperty dmProperty = TestUtilsShared.CreateUnsavedValidDMProperty();
            IDMClass dmClass = dmProperty.Class;
            DMRelationship dmRelationship = new DMRelationship();
            dmClass.OwnerRelationships.Add(dmRelationship);
            DMRelationshipProperty relationshipProperty = dmRelationship.RelationshipProperties.CreateBusinessObject();
            relationshipProperty.Property = dmProperty;
            return (DMClass) dmClass;
        }

        #endregion
    }

}